<?php
/**
 * @Name: BaseLogic.php
 * @Author: yashuai<1762910894@qq.com>
 */

namespace app\common\logic;

use HugCode\PhpUnits\Frame\Code\ConstCode;

abstract class BaseCurdLogic extends BaseLogic
{

    abstract protected function getParamsTableData(array $params, ?string $type = '', ?array $row = []): array;

    /**
     * @Desc 添加记录
     * @param array $params
     * @param bool $turn 参数是否需要转换
     * @return int|string
     * @author yashuai<1762910894@qq.com>
     */
    public function createRow(array $params = [], bool $turn = true)
    {
        $data = $turn ? $this->getParamsTableData($params, 'create') : $params;
        $data = $this->timestamp($data);
        if (!$insertId = $this->getModel()->insertGetId($data)) {
            _throw('添加失败', 105002);
        }
        return $insertId;
    }

    /**
     * @Desc 批量插入数据
     * @param array $data
     * @param array $merge 合并数据
     * @return false|int
     * @author yashuai<1762910894@qq.com>
     */
    public function createRowAll(array $data = [], array $merge = [])
    {
        if (empty($data)) return false;

        try {
            if (!empty($merge)) {
                foreach ($data as &$val) {
                    $val = array_merge($val, $merge);
                }
            }
            $data = $this->timestamp($data);
            return $this->getModel()->insertAll($data);
        } catch (\Exception $e) {
            _throw($e->getMessage() . '--' . $e->getFile(), 105002);
        }
        return false;
    }

    /**
     * @Desc 更新记录
     * @param array $params
     * @param array $where
     * @param bool $turn
     * @return \think\Model
     * @author yashuai<1762910894@qq.com>
     */
    public function updateRow(array $params = [], array $where = [], bool $turn = true)
    {
        $pk         = $this->getPk();
        $where[$pk] = $params[$pk];
        $row        = $this->validatorRow($where, ['*']);
        $data       = $turn ? $this->getParamsTableData($params, 'update', $row) : $params;
        $data       = $this->timestamp($data, 'update');
        return $this->getModel()->where($where)->update($data);
    }

    /**
     * @Desc 添加或者更新
     * @param array $where
     * @param array $data
     * @param array $create_data 如果为真，插入时用此数据
     * @return bool|int|string
     * @author yashuai<1762910894@qq.com>
     */
    public function createOrUpdate(array $where, array $data, array $create_data = [])
    {
        if (empty($this->get($where))) {
            $data = !empty($create_data) ? $create_data : $data;
            $data = $this->timestamp(array_merge($where, $data));
            return $this->createRow($data, false);
        } else {
            $data = $this->timestamp($data, 'update');
            $this->update($where, $data);
            return true;
        }
    }

    /**
     * @Desc 删除记录
     * @param $id
     * @param array $where
     * @return false|\think\Model
     * @author yashuai<1762910894@qq.com>
     */
    public function deleteRow($id, array $where = [])
    {
        $where = array_merge($where, $this->idWhere($id));
        if (empty($where)) {
            return false;
        }
        $this->validatorRow($where);
        return $this->update($where, ['is_del' => ConstCode::IS_YES]);
    }


}
